package cn.change.org.service.impl;

import cn.change.basic.service.impl.BaseServiceImpl;
import cn.change.basic.util.PageList;
import cn.change.org.domain.Department;
import cn.change.org.mapper.DepartmentMapper;
import cn.change.org.service.lDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 16252
 * @Transactional事物的传播机制：保证一组操作只有一个操作
 * 部门服务实现类
 */
@Service()
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements lDepartmentService {
    @Autowired
    private  DepartmentMapper  departmentMapper;
    /**
     * 无限极
     * @return
     */
    @Override
    public List<Department> queryTree() {
        /*第一步查询所有*/
        List<Department> all = departmentMapper.loadAll();
        /*从所有数据中找到父类 返回*/
        /*只要不是第一层级，就去找自己的父级并将自己放入上级的children里*/
        List<Department> first = new ArrayList<>();
        for(Department department : all){
            if (department.getParent_id() == null){
                first.add(department);
            }else { //其他层级
                for (Department parent : all) {
                    if (parent.getId() == department.getParent_id()){
                        parent.getChildren().add(department);
                        break; //结束内层循环
                    }
                }

            }
        }
        return first;
    }

}
